package ils.web.servlets.issue;

import ils.di.ObjectBuilder;
import ils.di.ObjectBuilderFactory;
import ils.domain.catalog.model.Book;
import ils.domain.catalog.model.BookRepository;
import ils.domain.issue.model.Issue;
import ils.domain.model.User;
import ils.domain.ugc.model.Review;
import ils.domain.ugc.model.ReviewRepository;
import ils.services.IssueService;
import ils.util.Util;
import ils.web.model.MyIssue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: Rejeev Divakaran
 * Date: 8/2/13
 */
@WebServlet(name = "MyBorrowingsServlet")
public class MyBorrowingsServlet extends HttpServlet {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyBorrowingsServlet.class);
    private IssueService issueService;
    private BookRepository bookRepository;
    private ReviewRepository reviewRepository;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer userId = ((User)request.getSession().getAttribute("user")).getId();
        List<Issue> issues = issueService.getAUsersOpenIssues(userId);
        List<MyIssue> myIssueses = constructIssueDetails(issues, userId);
        request.setAttribute("issues", myIssueses);
        LOGGER.debug("MyBorrowings: userId: " + userId + " issues: " + myIssueses);
        request.getRequestDispatcher("/issue/myborrowings.jsp").forward(request, response);
    }

    @Override
    public void init(){
        ObjectBuilder objectBuilder = ObjectBuilderFactory.get();
        issueService = objectBuilder.getInstance(IssueService.class, true);
    }

    private List<MyIssue> constructIssueDetails(List<Issue> issues, int userId){
        List<Integer> bookIds = new ArrayList<>();
        for(Issue issue : issues){
            bookIds.add(issue.getItem().getBookId());
        }
        Map<Integer,Book> books = bookRepository.getBooks(bookIds);
        Map<Integer,Review> reviewsForUserForGivenBooks = reviewRepository.getReviewsForUserForGivenBooks(userId, bookIds);
        List<MyIssue> myIssues = new ArrayList<>();
        for(Issue issue : issues){
            MyIssue myIssue = new MyIssue();
            Book book = books.get(issue.getItem().getBookId());
            double avgRating = (double)book.getRatingsSum()/(double)book.getRatingsCount();
            myIssue.setAvgRating(Util.format(avgRating, 1));
            myIssue.setBookName(book.getTitle());
            myIssue.setDueDate(Util.formatToDate(issue.getDueDate()));
            myIssue.setId(issue.getId());
            myIssue.setIssueDate(Util.formatToDate(issue.getIssueDate()));
            myIssue.setUserRating(reviewsForUserForGivenBooks.get(issue.getItem().getBookId()).getRating());
            myIssues.add(myIssue);
        }
        return myIssues;
    }
}
